package com.wd.mylibrary_login.activity

import android.util.Log

import android.widget.Toast
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route

import com.wd.mylibrary_base.base.BaseActivity
import com.wd.mylibrary_base.utils.HttpUtils
import com.wd.mylibrary_base.utils.MyApp
import com.wd.mylibrary_login.R
import com.wd.mylibrary_login.databinding.ActivityLoginDengBinding
import com.wd.mylibrary_login.viewmodel.LoginDengViewModel

@Route(path = "/login/Deng")
class Login_DengActivity : BaseActivity<LoginDengViewModel,ActivityLoginDengBinding>(R.layout.activity_login_deng){
    override fun initView() {
        dataBinding.loginButton.setOnClickListener  {



            var pwd= dataBinding.loginPwd.text.toString()
            var user= dataBinding.loginUser.text.toString()
            Log.i("TAG", pwd)
            Log.i("TAG", user)

            if(user.isEmpty()){
                Toast.makeText(this@Login_DengActivity, "邮箱账号不为空", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if(pwd.isEmpty()){
                Toast.makeText(this@Login_DengActivity, "密码不为空", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

              if(HttpUtils.httpUtils.isConnect(this)){
                  viewModel.getlogin(mapOf("userName" to dataBinding.loginUser.text.toString(), "passwd" to dataBinding.loginPwd.text.toString()))
              }


        }
    }

    override fun dataObserve() {
        viewModel.Login.observe(this, Observer{
            if(it.status.equals("0000")){
                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show()

                //存入 sp
                MyApp.sp.edit()
                    .putInt("userId",it.result.userId)
                    .putString("sessionId",it.result.sessionId)
                    .putString("headPic",it.result.headPic)
                    .putString("nickName",it.result.nickName)
                    .putString("workSum",it.result.workSum.toString())
                    .putString("signature",it.result.signature)
                    .putBoolean("isLogin",true)
                    .commit()

//                ARouter.getInstance().build("/main/MainActivity").navigation();


                finish()
            }
        })
    }





}